package servlet;

import bean.Hero;
import service.HeroService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@WebServlet("/HeroUpdateServlet")
public class HeroUpdateServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        try {
            //1. 获取客户端提交上来的数据
            int id = Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            float hp = Float.parseFloat(request.getParameter("hp"));
            int damage = Integer.parseInt(request.getParameter("damage"));
            //2. 添加到数据库
            //string -- date

            Hero hero = new Hero(id ,name, hp, damage);

            //2. 更新数据库数据
            HeroService service = new HeroService();
            service.update(hero);

            //3. 跳转界面
            request.getRequestDispatcher("HeroListServlet").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
